/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       materials.h

	$Header: /game/materials.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "timed_window.h"

#include "game_time.h"
// ------------------------------------------------------------------------
// window which appears for a fixed amount of time, and then closes itself.
// can also drift with time.
// ------------------------------------------------------------------------
t_timed_window::t_timed_window( t_screen_point point, t_window* parent, int lifetime, 
		                        t_screen_point drift, int drift_time )
			  : t_window( t_screen_rect(point.x, point.y, point.x, point.y), 
			              k_completely_transparent, parent )
{
	m_lifetime = lifetime;
	m_drift = drift;
	m_drift_delay = drift_time;
	m_last_update = get_time();
	m_last_drift_time = m_last_update;
}

// ------------------------------------------------------------------------
// window which appears for a fixed amount of time, and then closes itself.
// can also drift with time.
// ------------------------------------------------------------------------
void t_timed_window::update_size()
{
	set_size_from_children();
}

// ------------------------------------------------------------------------
// window which appears for a fixed amount of time, and then closes itself.
// can also drift with time.
// ------------------------------------------------------------------------
void t_timed_window::add_child( t_window* window )
{
	t_window::add_child( window );

	t_screen_rect rect = get_screen_rect();

	if (rect.height() == 0)
		rect = window->get_screen_rect();
	else
		rect = get_extent( rect, window->get_screen_rect() );

	// change window's size.
	move_screen_rect( rect, false );
}

// ------------------------------------------------------------------------
// window which appears for a fixed amount of time, and then closes itself.
// can also drift with time.
// ------------------------------------------------------------------------
void t_timed_window::on_idle()
{
	int elapsed = elapsed_time( m_last_update );

	m_last_update = get_time();
	m_lifetime -= elapsed;
	if (m_lifetime <= 0)
	{
		m_end_handler( this );
		close();
		return;
	}
	if (m_drift.x == 0 || m_drift.y == 0)
		return;
	if (elapsed_time( m_last_drift_time ) < m_drift_delay)
		return;
	m_last_drift_time = get_time();
	move_screen_rect( get_screen_rect() + m_drift );
}
